﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADTool
{
    public class Z
    {
        public static DocumentCollection dm { get { return Application.DocumentManager; } }
        public static Document doc { get { return dm.MdiActiveDocument; } }
        public static Database db { get { return doc.Database; } }
        public static Editor ed { get { return doc.Editor; } }
        public static Transaction trans { get { return db.TransactionManager.StartTransaction(); } }
        /// <summary>
        /// 获取系统变量
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static object GetSysVar(string name)
        {
            return Application.GetSystemVariable(name);
        }
        /// <summary>
        /// 设置系统变量
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetSysVar(string name, object value)
        {
            Application.SetSystemVariable(name, value);
        }
        public static void EntityAddModelSpace(Entity ent)
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                ent.SetDatabaseDefaults();
                btr.AppendEntity(ent);
                trans.AddNewlyCreatedDBObject(ent, true);
                trans.Commit();
            }
        }
        public static void ListAddModelSpace<T>(List<T> ents) where T : Entity
        {
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                foreach (var ent in ents)
                {
                    ent.SetDatabaseDefaults();
                    btr.AppendEntity(ent);
                    trans.AddNewlyCreatedDBObject(ent, true);
                }
                trans.Commit();
            }
        }
        /// <summary>
        /// 选择实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <returns>返回Entity</returns>
        public static List<T> PinkList<T>() where T : Entity
        {
            T ent;
            List<T> ents = new List<T>();
            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status == PromptStatus.OK)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    SelectionSet sSt = psr.Value;
                    foreach (ObjectId id in sSt.GetObjectIds())
                    {
                        ent = (T)trans.GetObject(id, OpenMode.ForWrite, false);
                        if (ent != null) ents.Add(ent);
                    }
                    trans.Commit();
                }
            }
            return ents;
        }
        /// <summary>
        /// 根据getdouble使程序暂停，按住esc程序继续，输入1程序中断，同时可以查看一个变量的值，并输出一个提示文字
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="str"></param>
        /// <exception cref="Exception"></exception>
        public static void Stop_EscGo_Input1Termination<T>(T t, string str)
        {
            ed.WriteMessage("\n***{0}:-->{1}\n", str, t);
            System.Windows.Forms.Application.DoEvents();
            double d = ed.GetDouble("").Value;
            if (d == 1) throw new Exception("已经手动退出！");
        }

    }
}
